package com.yaglyf.mvc.util;

import java.security.MessageDigest;

/**
 * Title: Class CommentsShow
 * Description:
 * 注释展示类，说明各种方法注释怎么写
 *
 * @author qiang.yang
 * @version 1.0.0
 * @email qiang.yang@chinaredstar.com
 * @date: 2016/10/14 16:23
 */
public class MD5 {

    public static String security(String key) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            return "";
        }
        char[] charArray = key.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = md5Bytes[i] & 0xFF;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }


    public static String security(String passWord, String salt) {
        try {
            if (salt.length() > 8) {
                passWord = salt.substring(0, 8) + passWord + salt.substring(salt.length() - 9, salt.length() - 1);
            }
            return security(passWord);
        } catch (Exception e) {
        }
        return "";
    }
}
